﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Angel.Entities;
using Angel.Daos.IManager;
using Angel.Daos.Manager;
using Angel.Web.Models; 
namespace Angel.Web.Controllers
{
    public class GroupController : Controller
    {
        private IUserManager userManager;
        private IGroupManager groupManager;
        private IGroup_UserManager groupUserManager;

        public GroupController()
            : this(new UserManager(), new GroupManager(), new Group_UserManager())
        {
        }
        public GroupController(IUserManager _userManager, IGroupManager _groupManager,IGroup_UserManager _groupUserManager)
        {
            userManager = _userManager;
            groupManager=_groupManager;
            groupUserManager = _groupUserManager;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        } 

        [HttpPost]
        public ActionResult Create(CreateGroupModel createGroupModel)
        {
            try
            {
                Group group = new Group();
                group.GroupName = createGroupModel.NameGroup;
                groupManager.Add(group);
                return RedirectToAction("Index");
            }
            catch(Exception ex)
            {
                ViewBag.MessageError = ex.Message;
            }
            return View();
        }

        [HttpGet]
        public ActionResult Delete(int? id)
        {
            try
            {
                if (id == null)
                {
                    return RedirectToAction("NotFound", "General", "Error id");
                }
                var group = groupManager.GetByID(id.Value);
                DeleteGroupModel deleteGroupMode = new DeleteGroupModel();
                deleteGroupMode.id = group.GroupID;
                return View(deleteGroupMode);
            }
            catch
            {
                return RedirectToAction("index", "General", @"Error definition");
            }
        }

        [HttpPost]
        public ActionResult Delete(DeleteGroupModel deleteGroupModel)
        {
            try
            {
                Group group = groupManager.GetByID(deleteGroupModel.id);
                groupManager.Delete(group);
            }
            catch (Exception ex)
            {
                ViewBag.MessageError = ex.Message;
            }

            return View("Index");
        }

        [HttpPost]
        public ActionResult Search(SearchGroupModels searchGroupModel)
        {
            //Output to Output Windows.
            System.Diagnostics.Debug.Write("HTTP POST");

            IEnumerable<Group> result = groupManager.Search(searchGroupModel.NameGroup);
            ViewBag.result = result;
            return View();
        }

        public ActionResult Search()
        {
            return View();
        }

        public String SearchXML(String name)
        {
            String txtResult = "";
           
            IEnumerable<Group> result = groupManager.Search(name.Trim());

            txtResult += "<Groups>";
            foreach (Group item in result)
            {
                txtResult += "<Group>";
                txtResult += "<Id>" + item.GroupID.ToString() + "</Id>";
                txtResult += "<Name>" + item.GroupName.ToString() + "</Name>";
                txtResult += "</Group>";
            }
            txtResult += "</Groups>";
            
            System.Diagnostics.Debug.Write(txtResult);
            return txtResult;
        }

        public ActionResult Edit()
        {
            IEnumerable<Group_User> list = groupManager.GetData();
            return View(list);
        }

        public ActionResult DeleteMember(int id)
        {
            var delMem = groupUserManager.GetByID(id);
            return View(delMem);
        }

        [HttpPost]
        public ActionResult DeleteMember(Group_User groupUser)
        {
            groupUserManager.Delete(groupUser);
            return RedirectToAction("Edit", "Group");
        }

        public ActionResult AddMember()
        {
            return View();
        }
    }
}
